//
//  MSFoodLoactionView.m
//  MSJD
//
//  Created by xiangbo on 2018/9/29.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSFoodLoactionView.h"
#import "MSMapsView.h"
#import "UILabel+AutoSize.h"

@interface MSFoodLoactionView ()<MSDetailViewDelegate>

@property (nonatomic, strong)MSMapsView *mapsView;
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *descLabel;
@property (nonatomic, strong)UIButton *moreButton;
@property (nonatomic, weak)id modelData;

@end

@implementation MSFoodLoactionView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubViews];
    }
    return self;
}

- (void)setupSubViews{
    UIView *spaceView = [UIView new];
    spaceView.backgroundColor = UIColorHex(0xf2f2f2);
    
    [self addSubview:self.mapsView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.descLabel];
    [self addSubview:self.moreButton];
    [self addSubview:spaceView];
    
    [self.mapsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self).offset(20);
        make.right.equalTo(self).offset(-20);
        make.height.equalTo(self.mapsView.mas_width).multipliedBy(88/335.f);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mapsView.mas_bottom);
        make.left.right.equalTo(self.mapsView);
        make.height.mas_equalTo(54);
    }];
    
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.left.right.equalTo(self.titleLabel);
    }];
    
    [self.moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.descLabel.mas_bottom).offset(10);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(30);
    }];
    
    [spaceView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(10);
    }];
}

- (void)configData:(id)data indexPath:(NSIndexPath *)indexPath{
    self.modelData = data;
    if ([data isKindOfClass:MSFoodDetailData.class]) {
        MSFoodDetailData *detailData = data;
        if (detailData.height == 0) {
            CGFloat height = [UILabel getHeightByWidth:MS_SCREEN_WIDTH - 30 title:detailData.intro font:self.descLabel.font];
            CGFloat spaceHeight = 104;
            if(!detailData.localExpand){
                if (height > 15 * 3) {
                    spaceHeight += 33;
                }else{
                    detailData.localExpand = YES;
                }
                height = MIN(height, 15 * 3);
            }
            detailData.height = height + spaceHeight + MS_SCREEN_WIDTH * (88/335.f);
        }
        if (detailData.localExpand) {
            self.descLabel.numberOfLines = 0;
            self.moreButton.hidden = YES;
        }else{
            self.moreButton.hidden = NO;
            self.descLabel.numberOfLines = 3;
        }
        
        self.descLabel.text = detailData.intro;
        CLLocation *loc = [[CLLocation alloc] initWithLatitude:detailData.lat.floatValue longitude:detailData.lon.floatValue];
        [self.mapsView gotoLocation:[[CLLocation alloc] initWithLatitude:detailData.lat.floatValue - 0.5 longitude:detailData.lon.floatValue - 0.5] to:loc];
    }
}

- (void)expandButtonClick:(id)sender{
    MSFoodDetailData *detailData = self.modelData;
    self.descLabel.numberOfLines = 0;
    CGFloat height = [UILabel getHeightByWidth:MS_SCREEN_WIDTH - 30 title:self.descLabel.text font:self.descLabel.font];
    detailData.height = height + 104 + MS_SCREEN_WIDTH * (88/335.f);
    detailData.localExpand = YES;
    [self routerEventWithName:kMSLoacationExpand userInfo:nil];
}

#pragma mark -Property
- (UIView *)mapsView{
    if (!_mapsView) {
        _mapsView = [[MSMapsView alloc] init];
        _mapsView.backgroundColor = MSSubTitleColor;
    }
    return _mapsView;
}

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font =[UIFont boldSystemFontOfSize:16.f];
        _titleLabel.text = @"商家介绍";
        _titleLabel.textColor = UIColorHex(0x444444);
    }
    return _titleLabel;
}


-(UILabel *)descLabel{
    if (!_descLabel) {
        _descLabel = [[UILabel alloc] init];
        _descLabel.numberOfLines = 3;
        _descLabel.font = [UIFont systemFontOfSize:12];
        _descLabel.textColor =[UIColor colorWithHexString:@"#444444"];
        _descLabel.text= @"这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复,这是一个e很长很长的描述,不断重复";
    }
    return _descLabel;
}

-(UIButton *)moreButton{
    if (!_moreButton) {
        _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_moreButton setTitle:@"展开全部" forState:UIControlStateNormal];
        [_moreButton setTitleColor:[UIColor colorWithHexString:@"#F96332"] forState:UIControlStateNormal];
        _moreButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_moreButton addTarget:self action:@selector(expandButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreButton;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
